<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<style>
.answer ul {list-style: none}
</style>
<div class="middle padtop10 myclear">
	<div class="page-content">
		
		<!-- bread crumb -->
		<p id="bread-crumb">
			<a href="<?php echo base_url();?>">
				<?php echo $lang['home']?>
			</a>
			<a href="<?php echo site_url('faqs');?>">
				<?php echo $lang['faqs'];?>
			</a>
			<?php echo $lang['manager_category'];?>
		</p>
		<!-- end bread crumb -->
		<h3>
			<?php echo $lang['manager_category'];?>
		</h3>
		<br style="clear:both;" />
		<table width="100%">
			<tr>
				<td style="text-align:right">
					<input type="button" value="<?php echo $lang['add'];?>" onclick="window.location.href='<?php echo site_url('faqs/addCategory');?>';" />
				</td>
			</tr>
		</table>
		<br />
		<?php if(count($lstCat) > 0){?>
		<ul>
			<?php
			$obj = new stdClass();
			$obj->level = 0;
			$obj->parent = 0;
			foreach ($lstCat as $item){
				if($item->parent_id == 0)
				{
					$obj->level = 0;
					$obj->parent = 0;
				}
				else if($obj->parent != $item->parent_id)
				{
					$obj->level += 1;
					$obj->parent = $item->parent_id;
				}
			?>
			<li style="padding-left:<?php echo ($obj->level * 20);?>px" class="catid<?php echo $item->cat_id;?>">
				<a href="javascript:void(0);" onclick="changeCategory(<?php echo $item->cat_id;?>);">
					<?php echo $item->name;?>
				</a>
			</li>
			<?php }?>
		</ul>
		<?php }?>
	</div>
</div><br />
<script type="text/javascript">
current_id = 0;
function changeCategory(id)
{
	if(current_id > 0 && current_id != id)
	{
		closeChange(current_id);
	}
	
	var text = $('input.name_catid'+id).val();
	if(text == 'undefined' || text == null)
	{
		var a = $('li.catid'+id);
		var curr_text = a.children('a').html();
		a.html('<input type="text" size="40" class="name_catid'+id+'" value="'+curr_text+'" /><input type="hidden" value="'+curr_text+'" />&nbsp;<input type="button" onclick="changeCategory('+id+');" value="<?php echo $lang['save'];?>" /><input type="button" onclick="closeChange('+id+');" value="<?php echo $lang['cancel'];?>" />');
	}
	else
	{
		if(text == '') {alert('<?php echo $lang['enter_name'];?>');return;}
		$.ajax({
			url: '<?php echo site_url('faqs/updateCategoryName');?>',
			type: 'post',
			data: {id: id, text: text},
			success: function(data){
				if(data == '1')
				{
					$('li.catid'+id).children('input:hidden').val(text);
					closeChange(id);
				}
				else alert(data);
			}
		});
	}

	current_id = id;
}
function closeChange(id)
{
	var a = $('li.catid'+id);
	var text = a.children('input:hidden').val();
	a.html('<a href="javascript:void(0);" onclick="changeCategory('+id+');">'+text+'<a>');
	current_id = 0;
}
</script>